package mouseEvents;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.swing.JPanel;

public class DrawLines extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Point[] nodes; 
	List<Line> a;
	int f =0 ;
	int d = 0;
	int[][] incedent =new int[Graph.Graph.maxVertexes][Graph.Graph.maxVertexes];
	 boolean fl ;
	private Point[] pLines1;
	private Point[] pLines2;
	int i = 0;
	int j = 0;

    private static final Color xorColor = Color.cyan;
    private static final Color paintColor = Color.blue;

    ImageC img;
    private HashSet<Point> point;


    public DrawLines(PointPanel pointPanel) {
    	fl = true;
    	img = new ImageC();
    	
    	a = new ArrayList<Line>();
        MouseHandler mh = new MouseHandler();
        addMouseListener(mh);
        addMouseMotionListener(mh);
        setBackground(Color.white);
        point = pointPanel.getHashSet();
        pLines1 = new Point[1000];
        pLines2 = new Point[1000];
        for(int z=0;z<1000;z++){
        	pLines1[z] = new Point(0,0);
        	pLines2[z] = new Point(0,0);
        }
        
    	
    }


  
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
         
		img.paintComponent(g);
		
        
        
        
        if(a.size()!=0){
     	   for(Line b: a){         	         	
         	b.paintComponent(g);
         	}
        }
        Graphics2D g2d = (Graphics2D) g;
        Paint p = g2d.getPaint();
        g2d.setColor(paintColor);
        
        for(Point p1:point){
        	
  			g2d.fillOval((int)p1.x-10,(int) p1.y-10, 20, 20);
    	}
  
        g2d.setPaint(p);
       
       g2d.setPaint(p);
    }
    
  
    class MouseHandler extends MouseAdapter {
    	
    	Line tmp;

    
       
        public void mousePressed(MouseEvent e) {
           
           
            Point press = new Point(e.getPoint());
            int R = 30;//radius of point
            Graphics2D g2d = (Graphics2D) getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Paint p =g2d.getPaint();
        
            BasicStroke pen1 = new BasicStroke(5);
    		g2d.setStroke(pen1);

    		g2d.setXORMode(xorColor);
    		
    		
            	for(Point p1:point){
    				
            			
            		if(((Math.abs(p1.x-press.x)*Math.abs(p1.x-press.x)+Math.abs(p1.y-press.y)*Math.abs(p1.y-press.y))<R*R)){
        				
            			g2d.setXORMode(xorColor);
            			g2d.setColor(Color.lightGray);
            			
            			if(d%2==0){
            				
            				
            				g2d.fillOval((int)p1.getX()-10, (int)p1.getY()-10, 20, 20);
            				pLines1[i] = new Point(p1);
            				++i;
            				++d;
            				
            			}
            			else{
            				
            				pLines2[j] =new Point( p1);
            				
                    		
                    		++j;
                    		++d;
            			
            				}
                   	
            		}
            	}
            	if((d%2==0)&&(d!=0)){
            				tmp = new Line(pLines1[i-1].x,pLines1[i-1].y,pLines2[j-1].x,pLines2[j-1].y);
            				a.add(new Line(pLines1[i-1].x,pLines1[i-1].y,pLines2[j-1].x,pLines2[j-1].y));//= );
            			
            				
            				g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            		         BasicStroke pen2 = new BasicStroke(5);
            		 		g2d.setStroke(pen2);
            			g2d.setColor(Color.blue);
            			
            				tmp.paintComponent(getGraphics());
            				 for(int k=0;k<i;k++){
            			        	
            			        	
            			        
            				 }
            			
            		
            	}
            	
            	g2d.setPaintMode();
                g2d.setPaint(p);
            		
            }
          
        public void mouseReleased(MouseEvent e) {
        	if((d%2==0)&&(d!=0)){
        		repaint();
        	}
        	
        }
        
 
        public void mouseMoved(MouseEvent e){
        	Graphics2D g2d = (Graphics2D) getGraphics();
            Paint p = g2d.getPaint();
            // compensating previous paint
            g2d.setXORMode(xorColor);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            BasicStroke pen1 = new BasicStroke(5);
    		g2d.setStroke(pen1);
    	   	Point motion = e.getPoint();
    	    
            BasicStroke pen2 = new BasicStroke(5);
    		g2d.setStroke(pen2);
    		g2d.setColor(Color.blue);
    		if(fl =false){
    	
    		g2d.setPaint(p);
           tmp =	new Line(pLines1[i-1].x,pLines1[i-1].y,motion.x,motion.y);
            tmp.paintComponent(getGraphics());
            
    		}
            g2d.setPaintMode();
            g2d.setPaint(p);
     
        
        	
        }
    }
     public int[][] generateIncedent(){
    	 for(int i=0;i<Graph.Graph.maxVertexes;i++){
 			for(int j=0;j<Graph.Graph.maxVertexes;j++){
 				incedent[i][j]=0;
 		
 			
 			}
 			
 			}
    	 
    	 int horiz=0;
    	 int vert = 0;
    	 boolean fl1 = false;
    	 boolean fl2 = false;
    	
    				 for(int z=0;z<i;z++){
    					 for(int k=0;k<point.size();k++){
    					
    				 if(pLines1[z].equals(nodes[k])){
    					 horiz = k;
    					 fl1 = true;
    					 
    					 
    				 }
    				 if(pLines2[z].equals(nodes[k])){
    					 vert = k;
    					 fl2= true;
    					 
    				 }
    				if(fl1&&fl2){
    				 incedent[horiz][vert] =1;
    				 incedent[vert][horiz] =1;
    				fl1=false;
    				fl2 = false;
    				 }
    				 }
    			}
    		
    	
    	 return incedent;
    	 
     } 
     public Point[] generatePoints(){
    	 nodes =  new Point[point.size()];
     	for(int z=0;z<point.size();z++){
     	nodes[z] =  new Point();
     	}
    	 int z = 0;
    	 for(Point p:point){
    		 
    		 nodes[z].x= (int) p.x;
    		 nodes[z].y= (int) p.y;
    		 
    		 
    		
    		 
    		 ++z;
    	 }
		return nodes;
    	 
     }
     public int sizeP(){
    	 return point.size();
     }
     

}
	

